package com.manor.dynamic.views;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;

import com.manor.common.utils.KLog;

import java.math.BigDecimal;

/**
 * Created by xcheng on 2016/6/3.
 */
public class ViewSizeChangeAnimation extends Animation {
    int initialHeight = 200;
    int targetHeight = 200;
    int initialWidth;
    int targetWidth;
    View view;
    int x;
    int y;

    public ViewSizeChangeAnimation(View view, int targetHeight, int targetWidth,int x, int y) {
        this.view = view;
        this.targetHeight = targetHeight;
        this.targetWidth = targetWidth;
        this.x = x;
        this.y = y;

    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = initialHeight + (int) ((targetHeight - initialHeight) * interpolatedTime);
        view.getLayoutParams().width = initialWidth + (int) ((targetWidth - initialWidth) * interpolatedTime);
//        FrameLayout.LayoutParams  params = (FrameLayout.LayoutParams) view.getLayoutParams();
//        params.setMargins(new BigDecimal(x+"").multiply(new BigDecimal(interpolatedTime+"")).negate().intValue(), new BigDecimal(y+"").multiply(new BigDecimal(interpolatedTime+"")).negate().intValue(), 0, 0);
//        KLog.e("tag",interpolatedTime+"//"+new BigDecimal(x+"").multiply(new BigDecimal(interpolatedTime+"")).negate().intValue());
//        view.setLayoutParams(params);
        ((FrameLayout.LayoutParams) view.getLayoutParams()).leftMargin = new BigDecimal(x+"").subtract(new BigDecimal(x+"").multiply(new BigDecimal(interpolatedTime+""))).intValue();
        ((FrameLayout.LayoutParams) view.getLayoutParams()).topMargin = new BigDecimal(y+"").subtract(new BigDecimal(y+"").multiply(new BigDecimal(interpolatedTime+""))).intValue();
        KLog.i("tag",
                interpolatedTime+"/"+y+"/"+new BigDecimal(y+"").multiply(new BigDecimal(interpolatedTime+"")).negate().intValue());
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        this.initialHeight = height;
        this.initialWidth = width;
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}